package my;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.util.GenericType;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(value = Parameterized.class)
public class WSTest {

	private static final String WS_URL = "http://localhost:8080/Library/";

	private static ILibrary library;

	@BeforeClass
	public static void setUpClass() throws Exception {
		library = ProxyFactory.create(ILibrary.class, WS_URL);
		Assert.assertNotNull(library);
		Assert.assertTrue(library.getBooks().isEmpty());
	}

	@AfterClass
	public static void tearDownClass() {
		for (Book book : library.getBooks()) {
			library.removeBook(book.getIsbn());
		}
	}

	@Parameters
	public static Collection<String[]> data() {
		String[][] data = new String[][] { { "WASAH", "Scary Movie" },
				{ "WTF", "O Mestre do Disfarce" },
				{ "Legend", "Shaolin Soccer" } };
		return Arrays.asList(data);
	}

	private final String isbn;

	private final String title;

	public WSTest(String isbn, String title) {
		this.isbn = isbn;
		this.title = title;
	}

	@Test
	public void testService() {
		Assert.assertNull(library.getBook(isbn));

		Assert.assertNotNull(library.addBook(isbn, title));

		Assert.assertNotNull(library.getBook(isbn));

		Assert.assertNotNull(library.removeBook(isbn));

		Assert.assertNull(library.getBook(isbn));
	}

	@Test
	public void testURL() throws Exception {
		// Check Books
		ClientRequest requestBook = new ClientRequest(WS_URL + "books");
		ClientResponse<List<Book>> responseBooks = requestBook
				.get(new GenericType<List<Book>>() {
				});
		Assert.assertEquals(responseBooks.getStatus(), 200);
		Assert.assertTrue(responseBooks.getEntity().isEmpty());

		// Insert book
		requestBook = new ClientRequest(WS_URL + "book/" + isbn + "?title="
				+ title);
		ClientResponse<Book> responseBook = requestBook.put(Book.class);
		Assert.assertEquals(responseBook.getStatus(), 200);
		Assert.assertNotNull(responseBook.getEntity());

		// Check Book
		requestBook = new ClientRequest(WS_URL + "book/" + isbn);
		responseBook = requestBook.get(Book.class);
		Assert.assertEquals(responseBook.getStatus(), 200);
		Assert.assertNotNull(responseBook.getEntity());

		// Remove Book
		responseBook = requestBook.delete(Book.class);
		Assert.assertEquals(responseBook.getStatus(), 200);
		Assert.assertNotNull(responseBook.getEntity());

		// Check Books
		requestBook = new ClientRequest(WS_URL + "books");
		responseBooks = requestBook.get(new GenericType<List<Book>>() {
		});
		Assert.assertEquals(responseBooks.getStatus(), 200);
		Assert.assertTrue(responseBooks.getEntity().isEmpty());
	}
}
